﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace OrderControl {
    public class OrderService {
        public static List<Order> GetAllOrders() {
            using (var db = new OrderModel()) {
                var query = db.Orders.Include("Customer").Include("Details").Include("Details.Commodity");
                return query.ToList();
            }
        }

        public static int AddOrder(Order order) {
            using (var db = new OrderModel()) {
                db.Orders.Add(order);
                db.SaveChanges();
            }
            return order.OrderId;
        }

        public static void DeleteOrder(int orderId) {
            using (var db = new OrderModel()) {
                var order = db.Orders.Include("Customer").Include("Details").FirstOrDefault(p => p.OrderId == orderId);
                if (order != null) {
                    db.Customers.Remove(order.Customer);
                    db.OrderDetails.RemoveRange(order.Details);
                    db.Orders.Remove(order);
                    db.SaveChanges();
                }
            }
        }

        public static Order SearchById(int orderId) {
            using (var db = new OrderModel()) {
                var order = db.Orders.Include("Customer").Include("Details").Include("Details.Commodity").FirstOrDefault(p => p.OrderId == orderId);
                return order;
            }
        }

        public static Order SearchByCommodity(string commodityName) {
            using (var db = new OrderModel()) {
                var order = db.Orders.Include("Customer").Include("Details").Include("Details.Commodity").FirstOrDefault(p => p.Details.Any(detail => detail.CommodityName.Contains(commodityName)));
                return order;
            }
        }

        public static Order SearchByCustomer(string customerName) {
            using (var db = new OrderModel()) {
                var order = db.Orders.Include("Customer").Include("Details").Include("Details.Commodity").FirstOrDefault(p => p.CustomerName.Contains(customerName));
                return order;
            }
        }

        public static Order SearchByPriceLower(double price) {
            using (var db = new OrderModel()) {
                var order = db.Orders.Include("Customer").Include("Details").Include("Details.Commodity").FirstOrDefault(p => p.TotalPrice < price);
                return order;
            }
        }

        public static Order SearchByPriceHigher(double price) {
            using (var db = new OrderModel()) {
                var order = db.Orders.Include("Customer").Include("Details").Include("Details.Commodity").FirstOrDefault(p => p.TotalPrice > price);
                return order;
            }
        }

        //public static void AddDetail(int orderId, OrderDetail detail) {
        //    using (var db = new OrderModel()) {
        //        var order = db.Orders.Include("Details").FirstOrDefault(p => p.OrderId == orderId);
        //        if (order != null) {
        //            order.Details.Add(detail);
        //            db.SaveChanges();
        //        }
        //    }
        //}

        //public static void DeleteDetail(int orderId, int detailId) {
        //    using (var db = new OrderModel()) {
        //        var order = db.Orders.Include("Details").FirstOrDefault(p => p.OrderId == orderId);
        //        if (order != null) {
        //            order.Details.Remove(order.Details.FirstOrDefault(p => p.OrderDetailId == detailId));
        //            db.SaveChanges();
        //        }
        //    }
        //}

        public static void Import() {
            using (FileStream fs = new FileStream("OrderService.xml", FileMode.Open)) {
                XmlSerializer serializer = new XmlSerializer(typeof(List<Order>));
                List<Order> orders = (List<Order>)serializer.Deserialize(fs);
                using (var db = new OrderModel()) {
                    foreach (Order order in orders) {
                        var oldOrder = db.Orders.Include("Details").FirstOrDefault(p => p.OrderId == order.OrderId);
                        if (oldOrder != null) {
                            db.Orders.Remove(oldOrder);
                        }
                        db.Orders.Add(order);
                    }
                    db.SaveChanges();
                }
            }
        }

        public static void Import(string filePath) {
            using (FileStream fs = new FileStream(filePath, FileMode.Open)) {
                XmlSerializer serializer = new XmlSerializer(typeof(List<Order>));
                List<Order> orders = (List<Order>)serializer.Deserialize(fs);
                using (var db = new OrderModel()) {
                    foreach (Order order in orders) {
                        var oldOrder = db.Orders.Include("Details").FirstOrDefault(p => p.OrderId == order.OrderId);
                        if (oldOrder != null) {
                            db.Orders.Remove(oldOrder);
                        }
                        db.Orders.Add(order);
                    }
                    db.SaveChanges();
                }
            }
        }

        public static void Export() {
            using (FileStream fs = new FileStream("OrderService.xml", FileMode.Create)) {
                XmlSerializer serializer = new XmlSerializer(typeof(List<Order>));
                serializer.Serialize(fs, GetAllOrders());
            }
        }

        public static void Export(string filePath) {
            using (FileStream fs = new FileStream(filePath, FileMode.Create)) {
                XmlSerializer serializer = new XmlSerializer(typeof(List<Order>));
                serializer.Serialize(fs, GetAllOrders());
            }
        }
    }
}
